query("SELECT DISTINCT academic_year FROM calender ORDER BY academic_year DESC"); $years = $stmt_years->fetchAll(PDO::FETCH_ASSOC); // Fetch classes $stmt_classes = $DBcon->query("SELECT DISTINCT classid FROM class ORDER BY classid"); $classes = $stmt_classes->fetchAll(PDO::FETCH_ASSOC); // Fetch subjects $stmt_subjects = $DBcon->query("SELECT subject_id, subjectname FROM subjectss ORDER BY subjectname"); $subjects = $stmt_subjects->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = "Error fetching dropdown data: " . $e->getMessage(); } // Handle form submission if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Get and sanitize form data $acyear = sanitize_input($_POST['acyear'] ?? ''); $klass = sanitize_input($_POST['klass'] ?? ''); $subject = sanitize_input($_POST['subject'] ?? ''); $term = sanitize_input($_POST['term'] ?? ''); // Validate all fields are selected if (empty($acyear) || empty($klass) || empty($subject) || empty($term)) { $error = "All fields are required!"; } else { // Check which button was clicked if (isset($_POST['preview'])) { // Preview button clicked - show records without deleting $show_preview = true; try { // Count records $count_stmt = $DBcon->prepare(" SELECT COUNT(*) as count FROM marks WHERE acyear = ? AND klass = ? AND subject = ? AND term = ? "); $count_stmt->execute([$acyear, $klass, $subject, $term]); $result = $count_stmt->fetch(PDO::FETCH_ASSOC); $record_count = $result['count']; if ($record_count > 0) { // Fetch preview records $preview_stmt = $DBcon->prepare(" SELECT sn, regno, test, exam, admin, created_at FROM marks WHERE acyear = ? AND klass = ? AND subject = ? AND term = ? ORDER BY regno LIMIT 50 "); $preview_stmt->execute([$acyear, $klass, $subject, $term]); $preview_data = $preview_stmt->fetchAll(PDO::FETCH_ASSOC); $message = "Found $record_count record(s) matching your criteria. Previewing first " . min(50, $record_count) . " records."; } else { $error = "No records found matching the selected criteria."; $show_preview = false; } } catch (PDOException $e) { $error = "Error fetching preview: " . $e->getMessage(); } } elseif (isset($_POST['delete']) && isset($_POST['csrf_token'])) { // Delete button clicked - perform deletion // CSRF protection if (!validate_csrf_token($_POST['csrf_token'])) { $error = "Invalid CSRF token."; } else { try { // Start transaction $DBcon->beginTransaction(); // First, count records that will be deleted $count_stmt = $DBcon->prepare(" SELECT COUNT(*) as count FROM marks WHERE acyear = ? AND klass = ? AND subject = ? AND term = ? "); $count_stmt->execute([$acyear, $klass, $subject, $term]); $result = $count_stmt->fetch(PDO::FETCH_ASSOC); $record_count = $result['count']; if ($record_count > 0) { // Delete records $delete_stmt = $DBcon->prepare(" DELETE FROM marks WHERE acyear = ? AND klass = ? AND subject = ? AND term = ? "); $delete_stmt->execute([$acyear, $klass, $subject, $term]); $DBcon->commit(); $message = "Successfully deleted $record_count record(s) for: Academic Year: $acyear, Class: $klass, Subject: $subject, Term: $term"; // Clear form after successful deletion $acyear = $klass = $subject = $term = ''; $preview_data = []; $show_preview = false; } else { $error = "No records found matching the selected criteria."; } } catch (PDOException $e) { $DBcon->rollBack(); $error = "Error deleting records: " . $e->getMessage(); } } } } } // Generate CSRF token $csrf_token = generate_csrf_token(); ?> Delete Marks - School Admin

Select Criteria for Deletion

Warning: Deletion is permanent. Preview records before deleting.

0): ?>

Preview of Records to be Deleted Total: record(s)

Showing first of records. 50): ?> Only first 50 records shown. All records will be deleted.
count($preview_data)): ?>
# Reg No Test Score Exam Score Total Admin Created At
Totals (Preview): Avg Total: 0 ? number_format($total_combined / count($preview_data), 2) : '0.00'; ?>
... and more record(s) not shown ...
Deletion Warning

You are about to delete record(s) permanently.
This action cannot be undone. Please verify the criteria is correct:

  • Academic Year:
  • Class:
  • Subject:
  • Term:

How to Use This Tool

Step-by-Step Process:
  1. Select Criteria: Choose Academic Year, Class, Subject, and Term
  2. Preview: Click "Preview Records" to see what will be deleted
  3. Review: Carefully check the preview table
  4. Confirm: If correct, click "Confirm Delete"
  5. Cancel: If not correct, change criteria and preview again
Safety Features:
  • Preview before deletion
  • Record count display
  • Confirmation dialogs
  • CSRF protection
  • Transaction rollback on error
Tip: Always preview before deleting to avoid accidental data loss.